Thread: [Inheritance Hierarchy] User Input on program with constructors. How ?

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    4

    Question [Inheritance Hierarchy] User Input on program with constructors. How ?

    Hie there everyone, i'm currently working on my assignment on Inheritance Hierarchy. I managed to construct everything, the program is fully working without any problem.

    BUT now i need to make a user input based program instead of test program like what i've done. Its like, the user can input the receiver and sender name and informations. I thought this would be the easiest part but damn.. i'm trying everything i know trying to link the >>cin to the constructors above but none are working. Everything giving me error. Again and again.. errors.. errors.. errors..

    Please help me. Any idea how to do it ? An example on how to create a user input for atleast the sender's name in this program would be very very much appreciated. The rest i can do it myself.

    Thanks.


    Code:
    #include <iostream>
    #include <iomanip>
    #include <string>
    
    
    using namespace std;
    
    
    class Package
    {
          
     public:
    // constructor initiliazes data members
    
                   Package( const string &, const string &, const string &, 
                   const string &, int, const string &, const string &, const string &, 
                   const string &, int, double, double );
    
                   void setSenderName( const string & ); 
                   string getSenderName() const;
    
                   void setSenderAddress( const string & );
                   string getSenderAddress() const; 
    
                   void setSenderCity( const string & ); 
                   string getSenderCity() const; 
    
                   void setSenderState( const string & ); 
                   string getSenderState() const; 
    
                   void setSenderZIP( int ); 
                   int getSenderZIP() const; 
    
                   void setRecipientName( const string & ); 
                   string getRecipientName() const; 
    
                   void setRecipientAddress( const string & ); 
                   string getRecipientAddress() const; 
    
                   void setRecipientCity( const string & ); 
                   string getRecipientCity() const; 
    
                   void setRecipientState( const string & ); 
                   string getRecipientState() const; 
    
                   void setRecipientZIP( int ); 
                   int getRecipientZIP() const; 
    
                   void setWeight( double ); 
                   double getWeight() const; 
    
                   void setCostPerOunce( double ); 
                   double getCostPerOunce() const; 
    
                   virtual double calculateCost() const; 
                   
     private:
    // data members to store sender and recipient's address information
    
                   string senderName;
                   string senderAddress;
                   string senderCity;
                   string senderState;
                   int senderZIP;
                   string recipientName;
                   string recipientAddress;
                   string recipientCity;
                   string recipientState;
                   int recipientZIP;
    
                   double weight; // weight of the package
                   double costPerOunce; // cost per ounce to ship the package
    
    };
    
    //-----------------------------------------------------------------------------------------------------------//
    
    class OvernightPackage : public Package
    {
          
     public:
           
           OvernightPackage( const string &, const string &, const string &, const string &, int, 
           const string &, const string &, const string &, const string &, int, double, double, double );
                   
           void setOvernightFeePerOunce( double ); 
           double getOvernightFeePerOunce() const; 
           virtual double calculateCost() const; 
     
     private:
           
           double overnightFeePerOunce; // fee per ounce for overnight delivery
           
    }; // end class OvernightPackage
    
    
    //-----------------------------------------------------------------------------------------------------------//
    
    
    // OvernightPackage constructor
    OvernightPackage::OvernightPackage( const string &sName, const string &sAddress, const string &sCity, 
    const string &sState, int sZIP, const string &rName, const string &rAddress, const string &rCity, 
    const string &rState, int rZIP, double w, double cost, double fee ) : Package( sName, sAddress, sCity, 
    sState, sZIP, rName, rAddress, rCity, rState, rZIP, w, cost )
    
    .
    ..
    ...
    
    //-----------------------------------------------------------------------------------------------------------//
    
    
    class TwoDayPackage : public Package
    {
          
     public:
           
           TwoDayPackage( const string &, const string &, const string &, const string &, int, 
           const string &, const string &, const string &, const string &, int, double, double, double );
    
           void setFlatFee( double ); 
           double getFlatFee() const; 
    
           virtual double calculateCost() const; 
    
     private:
             
            double flatFee; 
            
    }; 
    
    
    //-----------------------------------------------------------------------------------------------------------//
    
    
    // TwoDayPackage constructor
    TwoDayPackage::TwoDayPackage( const string &sName, const string &sAddress, 
    const string &sCity, const string &sState, int sZIP, const string &rName, const string &rAddress, 
    const string &rCity, const string &rState, int rZIP, double w, double cost, double fee ) 
    : Package( sName, sAddress, sCity, sState, sZIP, rName, rAddress, rCity, rState, rZIP, w, cost )
    
    .
    ..
    ...
    
    
    
    //-----------------------------------------------------------------------------------------------------------//
    
    
    // Package constructor initiliazes data members
    Package::Package( const string &sName, const string &sAddress, const string &sCity, const string &sState, 
    int sZIP, const string &rName, const string &rAddress, const string &rCity, const string &rState, int rZIP, 
    double w, double cost ) : senderName( sName ), senderAddress( sAddress ), senderCity( sCity ), 
    senderState( sState ), senderZIP( sZIP ), recipientName( rName ), recipientAddress( rAddress ), 
    recipientCity( rCity ), recipientState( rState ), recipientZIP( rZIP )
    
    .
    ..
    ...
    
    //-----------------------------------------------------------------------------------------------------------//
     
         
    int main()
    {
    
    //This is the test program
    OvernightPackage NightDeliver("Micheal", "Dr Rajkumar Road", "Rajajinagar Stage II", 
    "Bangalore", 12345, "Mathan", "Manjunath Road", "Berhampur", "Orissa", 54321, 16.00, 1.00, .25 );
    
    TwoDayPackage DayDeliver("Kamal", "Amravati Road", "Nagpur", "Maharashtra", 33333, 
    "Rajan", "Vijayawada Road", "Ansari Nagar", "New Delhi", 44444, 10.5, .65, 2.0 );
    cout << fixed << setprecision(2);             
    
    
    cout << "Customers for overnight delivery\n";
    cout << "\n";
    
    cout << "The sender :\n" << NightDeliver.getSenderName()<< "\n";
    cout << NightDeliver.getSenderAddress() << "\n";
    cout << NightDeliver.getSenderCity() << " " << NightDeliver.getSenderState() << " " 
    << NightDeliver.getSenderZIP() << "\n";
    cout << "\n";
    
    cout << "The recipient :\n" << NightDeliver.getRecipientName()<< "\n";
    cout << NightDeliver.getRecipientAddress() << "\n";
    cout << NightDeliver.getRecipientCity() << " " << NightDeliver.getRecipientState() << " " 
    << NightDeliver.getRecipientZIP() << "\n";
    cout << "The cost is $ " << NightDeliver.calculateCost() << "\n";
    
    cout << "\n\n";
    
    cout << "Customers for 2 days delivery\n";
    cout << "\n";
    
    cout << "The sender :\n" << DayDeliver.getSenderName()<< "\n";
    cout << DayDeliver.getSenderAddress() << "\n";
    cout << DayDeliver.getSenderCity() << " " << DayDeliver.getSenderState() << " " 
    << DayDeliver.getSenderZIP() << "\n";
    cout << "\n";
    
    cout << "The recipient :\n" << DayDeliver.getRecipientName()<< "\n";
    cout << DayDeliver.getRecipientAddress() << "\n";
    cout << DayDeliver.getRecipientCity() << " " << DayDeliver.getRecipientState() << " " 
    << DayDeliver.getRecipientZIP() << "\n";
    cout << "The cost is $ " << DayDeliver.calculateCost() << "\n";
    cout << "\n\n";
    
    
    system ("pause");
    return 0;
    
    }
    Last edited by chandreu; 04-25-2008 at 02:28 PM. Reason: Re-edited(removed some part of the code) again to protect from code theft by lazy arses.

  2. #2
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    Er... I didn't see a 'std::cin' anywhere. What are you trying?

    And also, can you edit your post and break the long lines? Your making my laptop cry.

    Soma

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I'm thinking that your package class could do with having the "where to send" and "where from" split out into a separate "address" class. You would then have two instances of that, sender and recipient in the package class, rather than repeating a bunch of member variables with sender and recipient prefixes. A further consequence of this would be that you could write a funciton to display an address, making your overall code a lot simpler.

    You may even find that you can deal with address data completely independently from the package class, and just pass in a sender and receiver address into the constructor, taking the number of arguments to the constructor down to something manageable.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    You have created perfectly named member functions. There's no need to adorn them with superfluous comments. I'd lose the comments at the end of every function, too.

    Code:
    // return sender's ZIP code
    int Package::getSenderZIP() const
    {
           return senderZIP;
    } // end function getSenderZIP

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    4
    Thanks for the replies. I edited my code as requested.

    Quote Originally Posted by phantomotap View Post
    Er... I didn't see a 'std::cin' anywhere. What are you trying?

    And also, can you edit your post and break the long lines? Your making my laptop cry.

    Soma
    Sorry for the long lines, i didn't break the lines before bcoz it makes the code looks messy.

    You wont find the 'std::cin' anywhere coz i'm using the namespace std therefore i don't need to declare the "std::cin" seperately.

    What i'm trying to ask here:

    Now you can see the constructor for each function TwoDayPackage and overNightPackage it goes on like

    OvernightPackage (const string &sName, const string &sAddress, const string &sCity, const string &sState, int sZIP.......)

    So, when i build my test program in int main() below, i simply create an object under that function and enter values that follow the same arrangement i made above (name, address, city, state, zip code.....)

    Now, what im trying to ask is, how to make user input the values into the function instead of declaring it in the program itself. For example,

    cout>> "please enter sender name : ";
    cin>>sName; <----- sName refering to sender name in the OverNightPackage

    Ofcoz it doesn't work simply this way as i'll definately will get "sName undeclared" (i'm just trying to give you a good picture about what i want my program to do). But there must be some way to achieve this thru other method. And all i'm asking for is this unknown method, how to do it ?

    You have created perfectly named member functions. There's no need to adorn them with superfluous comments. I'd lose the comments at the end of every function, too.
    i removed most of the comments, hope its much easier for the eyes now. It is for my stupid lecturer anyway, he wanted it that way.

    I'm thinking that your package class could do with having the "where to send" and "where from" split out into a separate "address" class. You would then have two instances of that, sender and recipient in the package class, rather than repeating a bunch of member variables with sender and recipient prefixes. A further consequence of this would be that you could write a funciton to display an address, making your overall code a lot simpler.

    You may even find that you can deal with address data completely independently from the package class, and just pass in a sender and receiver address into the constructor, taking the number of arguments to the constructor down to something manageable.
    Mats, it took me almost 20 minutes to figure out what you are trying to say. Your explaination is crystal clear but its just that i'm quite new to this programming stuff. I understand now what you are saying thou but it gonna take me alot lot more time and i think thats what most probably i gotta do since there are no other better solution around.

    I have 3 days remaining to complete this assignment.. plus another subject assignment.. gotta model a warrior character in 3ds max.. 4 days to deadline and i'm still figuring out what texture would fit on his butt... ;(

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    You can create extra variables to read the values into.
    Code:
    string sName;
    cin >> sName;
    OvernightPackage(sName, ...);
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  7. #7
    Registered User
    Join Date
    Apr 2008
    Posts
    4
    Quote Originally Posted by CornedBee View Post
    You can create extra variables to read the values into.
    Code:
    string sName;
    cin >> sName;
    OvernightPackage(sName, ...);
    CornedBee... what you mean by extra variable ? I don't quite get it. Would you mind explaining me about it ?

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    string sName;
    cin >> sName;
    In the above piece of code, we would be reading in the sender name.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Registered User
    Join Date
    Apr 2008
    Posts
    4
    Hey guys... its finally working the way i want it. Great thanks to Mats and CornedBee. When i read CornedBee, i didn't get what he meant TILL only after reading Mats short reply, it "clickED" at the back of my head !!! Tried it and works like wonders !!!!

    Thanks CornedBee !!!!
    Thanks Mats !!!

    This is the full user input code i did based on CornedBee's simple example.

    Code:
    void ChoiceOvernight()
    {
    
    string sName, sAddress, sCity, sState;
    int sZIP;
    
    string rName, rAddress, rCity, rState;
    int rZIP;
    
    double w, cost, fee;
    
           ofstream outfile("OvernightData.txt",ios::ate);
    
    	   cout<<"\t\t\t------------------------------------"<<endl;
    	   cout<<"\t\t\t    Over Night Package Delivery     "<<endl;
    	   cout<<"\t\t\t------------------------------------"<<endl<<endl;
    
    cout <<"Please enter sender name, address, city, state, zip, : "<<endl;
    cin >> sName >> sAddress >> sCity >> sState >> sZIP;
    
    cout <<"Please enter receiver name, address, city, state, zip : "<<endl;
    cin >> rName >> rAddress >> rCity >> rState >> rZIP;
    
    cout <<"Please enter the weight of the package : "<<endl;
    cin >> w;
    
    OvernightPackage NightDeliver(sName, sAddress, sCity, sState, sZIP, rName, 
    rAddress, rCity, rState, rZIP, w, cost, fee );
    cout << fixed << setprecision(2);   
    
    cout << "Information status for overnight delivery\n";
    cout << "\n";
    
    cout << "The sender :\n" << NightDeliver.getSenderName()<< "\n";
    cout << NightDeliver.getSenderAddress() << "\n";
    cout << NightDeliver.getSenderCity() << " " << NightDeliver.getSenderState() << 
    " " << NightDeliver.getSenderZIP() << "\n";
    cout << "\n";
    
    cout << "The recipient :\n" << NightDeliver.getRecipientName()<< "\n";
    cout << NightDeliver.getRecipientAddress() << "\n";
    cout << NightDeliver.getRecipientCity() << " " << NightDeliver.getRecipientState() << 
    " " << NightDeliver.getRecipientZIP() << "\n";
    
    cout << "The cost is $ " << NightDeliver.calculateCost() << "\n";
      
    outfile<<" Customers Information Status for Two Day Delivery"<<"\n"<<sName<<"\n"<<sAddress<<"\n"<<sCity<<" "<<sState<<" "<<
    sZIP<<"\n\n"<<rName<<"\n"<<rAddress<<"\n"<<rCity<<" "<<rState<<" "<<
    rZIP<<"\n\n"<<"Package Weight: "<<w<<"KG"<<"\n"<<"Total Charges: "<<
    "RM"<<cost<<"\n\n\n";
        
        outfile.close();
        system("PAUSE");
    	system("cls");
    }
    
    
    //-----------------------------------------------------------------------------------------------------------//
    
    
    void ChoiceTwoDay()
    {
    
    string sName, sAddress, sCity, sState;
    int sZIP;
    
    string rName, rAddress, rCity, rState;
    int rZIP;
    
    double w, cost, fee;
    
           ofstream outfile("TwoDayData.txt",ios::ate);
    
    	   cout<<"\t\t\t------------------------------------"<<endl;
    	   cout<<"\t\t\t      Two Day Package Delivery      "<<endl;
    	   cout<<"\t\t\t------------------------------------"<<endl<<endl;
    	   
    	   
    cout <<"Please enter sender name, address, city, state, zip, : "<<endl;
    cin >> sName >> sAddress >> sCity >> sState >> sZIP;
    
    cout <<"Please enter receiver name, address, city, state, zip : "<<endl;
    cin >> rName >> rAddress >> rCity >> rState >> rZIP;
    
    cout <<"Please enter the weight of the package : "<<endl;
    cin >> w;
    
    TwoDayPackage DayDeliver(sName, sAddress, sCity, sState, sZIP, rName, 
    rAddress, rCity, rState, rZIP, w, cost, fee );
    cout << fixed << setprecision(2);   
    
    
    cout << "\nInformation status for two days delivery\n";
    cout << "\n";
    
    cout << "The sender :\n" << DayDeliver.getSenderName()<< "\n";
    cout << DayDeliver.getSenderAddress() << "\n";
    cout << DayDeliver.getSenderCity() << " " << DayDeliver.getSenderState() <<
     " " << DayDeliver.getSenderZIP() << "\n";
    cout << "\n";
    
    cout << "The recipent :\n" << DayDeliver.getRecipientName()<< "\n";
    cout << DayDeliver.getRecipientAddress() << "\n";
    cout << DayDeliver.getRecipientCity() << " " << DayDeliver.getRecipientState() << " " << DayDeliver.getRecipientZIP() << "\n";
    cout << "The cost is $ " << DayDeliver.calculateCost() << "\n";
    cout << "\n";
      
    outfile<<" Customers Information Status for Two Day Delivery"<<"\n"<<sName<<"\n"<<sAddress<<"\n"<<sCity<<" "<<sState<<" "<<
    sZIP<<"\n\n"<<rName<<"\n"<<rAddress<<"\n"<<rCity<<" "<<rState<<" "<<
    rZIP<<"\n\n"<<"Package Weight: "<<w<<"KG"<<"\n"<<"Total Charges: "<<
    "RM"<<cost<<"\n\n\n";
        
        outfile.close();
        system("PAUSE");
    	system("cls");
    }
    
    int main()
    {
    
    int target=0;
        double amount;
        char opt='x';
        
     while(opt=='x'|| opt=='X')
     {
    
          cout<<"   1.--> [ Over Night Delivery ]         ";                                           
          cout<<"   2.--> [ Two Day Delivery ]            ";                                        
          cout<<"   3.--> [ EXIT ]                        ";
    
    
          cout<<endl;
          cout<<"\t\t\tEnter Selection from (1 - 3) ==>";
          cin>> target; 
          cout<<endl;
    	if (target==1)
    	{ 
             system("cls");
             ChoiceOvernight();
             }          
        else if (target==2)
        {    
             system("cls");
             ChoiceTwoDay();
             }
        else if (target==3)
           {
             exit(0);
             }
             }
             }
    I removed some part of my original code from the first post, worried if some of my lazier classmates googled, came across this code and submit it as their own. Hope its not against this forum rules or anything.


    Thanks to you guys, now i can go back to my 3Ds Max and start editing my warrior's butt...
    Last edited by chandreu; 04-25-2008 at 03:01 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can someone help me with these errors please code included
    By geekrockergal in forum C Programming
    Replies: 7
    Last Post: 02-10-2009, 02:20 PM
  2. Client-server system with input from separate program
    By robot-ic in forum Networking/Device Communication
    Replies: 3
    Last Post: 01-16-2009, 03:30 PM
  3. SSH Hacker Activity!! AAHHH!!
    By Kleid-0 in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 03-06-2005, 03:53 PM